<?php
namespace app\admin\controller;

use think\Db;
use think\Loader;
use think\Request;

class Auth extends Base
{
    /**
     * 管理员列表
     */
    public function adminList()
    {
        $userListArr = model('AdminUsers')->userList();
        $this->assign('userListArr',$userListArr);
        return $this->fetch();
    }

    /**
     * 增加权限页面
     */
    public function adminRule()
    {
        $ruleListArr = model('AdminAuthRule')->getTreeData('tree','id','title');
        $assign=array(
            'ruleListArr'=>$ruleListArr
        );
        $this->assign($assign);
        return $this->fetch();
    }

    /**
     * 增加权限
     */
    public function adminRuleAdd(Request $request)
    {
        if ($request->isPost()) {
            $data = $request->post();
            $res = model('AdminAuthRule')->addRule($data);
            if (!$res) {
                return ['status' => 0, 'msg' => '添加失败'];
            } else {
                return ['status' => 1, 'msg' => '添加成功'];
            }
        }
    }

    /**
     * 增加用户
     */
    public function adminAdd(Request $request)
    {
        if ($request->isPost())
        {
            $data = $request->post();
            $group = explode(',',$data['ids']);
            $data = [
                'username' => $data['username'],
                'phone' => $data['phone'],
                'email' => $data['email'],
                'password' => $data['password'],
                'repassword' => $data['repassword'],
            ];

            $validate = Loader::validate('UserInfo');
            if (!$validate->check($data))
            {
                return ['status' => 0, 'msg' => '添加失败', 'result' => $validate->getError()];
            } else{
                unset($data['repassword']);
                $data['register_time'] = time();
                $data['last_login_ip'] = '';
                $data['last_login_time'] = 0;
                //添加用户
                $res1 = model('AdminUsers')->addUser($data);
                if ($res1)
                {
                    $lists = array();
                    foreach ($group as $id)
                    {
                        array_push($lists,array('uid'=>$res1, 'group_id'=>$id));
                    }
                    //为用户添加角色
                    $res2 = Db::name('admin_auth_group_access')->insertAll($lists);

                    if (!$res2)
                    {
                        return ['status' => 0, 'msg' => '程序错误，请联系管理员！'];
                    } else {
                        return ['status' => 1, 'msg' => '添加成功'];
                    }
                } else {
                    return ['status' => 0, 'msg' => '程序错误，请联系管理员！'];
                }
            }
        } else {
            $authGroupArr = model('AdminAuthGroup')->authGroupData();
            $assign = array(
                'authGroupArr'=>$authGroupArr,
            );
            $this->assign($assign);
            return $this->fetch();
        }

    }

    /**
     * 编辑页面
     */
    public function adminEdit()
    {
        return $this->fetch();
    }

    /**
     * 角色管理
     */
    public function adminRole()
    {
        $authGroupArr = model('AdminAuthGroup')->authGroupData();
        $assign = array(
            'authGroupArr'=>$authGroupArr,
        );
        $this->assign($assign);
        return $this->fetch();
    }

    /**
     * 添加角色
     */
    public function roleAdd(Request $request)
    {
        if($request->isPost())
        {
            //添加规则
            $data=$request->post();
            $data['rules'] = $data['ids'];
            unset($data['ids']);
            $res=model('AdminAuthGroup')->editAuthGroupData($data);
            if (!$res)
            {
                return ['status' => 0, 'msg' => '程序错误，请联系管理员！'];
            } else{
                return ['status' => 1, 'msg' => '添加成功'];
            }
        } else {
            //修改规则
            $group_data = array(
                'id'=>0,
                'rules'=>array(),
                'title'=>'',
                'desc'=>''
            );
            if($id = $request->get('id'))
            {
                $group_data=Db::name('admin_auth_group')->where(array('id'=>$id))->find();
                $group_data['rules']=explode(',', $group_data['rules']);
            }

            // 获取规则数据
            $rule_data=model('AdminAuthRule')->getTreeData('level','id','title');
            $assign=array(
                'group_data'=>$group_data,
                'rule_data'=>$rule_data
            );
            $this->assign($assign);
            return $this->fetch();
        }
    }

}
